/**
 * 
 */
package com.ekingstar.framework.action;

import org.apache.struts2.config.Action;
import org.apache.struts2.config.Result;
import org.apache.struts2.dispatcher.ServletActionRedirectResult;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;
import com.ekingstar.framework.domain.Role;
import com.ekingstar.framework.service.IRoleService;
import com.ekingstar.framework.service.RoleExistException;

/**
 * 
 *
 */
@Action(name="addOrUpdate",namespace="/role")
@Result(name="list",value="list",type=ServletActionRedirectResult.class,params={"namespace","/role"})
public class AddOrUpdateRoleAction extends ActionSupport{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private IRoleService roleService;
	
	public Role getRole() {
		return role;
	}
	public void setRole(Role role) {
		this.role = role;
	}
	public void setRoleService(IRoleService roleService) {
		this.roleService = roleService;
	}
	private Role role;
	
	
	
	
	public String input(){
		if (role != null && role.getId() !=null){
			role = roleService.viewRole(role.getId());
		}
		return "input";
	}
	@Validations(requiredStrings={@RequiredStringValidator(message="角色名不能为空",fieldName="role.name")})
	public String addOrUpdate(){
		try{
		if (role != null && role.getId() != null && !role.getId().trim().equals("")){
			roleService.updateRole(role);
		}
		else {
			roleService.addRole(role);
		}
		}catch(RoleExistException e){
			this.addActionError(e.getMessage());
			return "input";
		}
		
		return "list";
	}
}
